package com.tql.mall.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tql.mall.model.enums.merchant.LoginFailEnum;
import com.tql.mall.model.enums.merchant.RegiFailEnum;
import com.tql.mall.model.enums.merchant.StatusEnum;
import com.tql.mall.model.merchant.Merchant;
import com.tql.mall.model.merchant.MerchantLoginDto;
import com.tql.mall.model.merchant.MerchantLoginVo;
import com.tql.mall.model.merchant.MerchantRegisterVo;
import com.tql.mall.user.mapper.MerchantMapper;
import com.tql.mall.user.service.MerchantService;
import com.tql.mall.user.utils.VertifyUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Date;

@Service
public class MerchantServiceImpl extends ServiceImpl<MerchantMapper, Merchant> implements MerchantService {

    @Override
    public String register(MerchantRegisterVo merchantRegisterVo) {
        //防空
        if (!VertifyUtil.isInfoComplete(merchantRegisterVo)){
            return RegiFailEnum.FAIL_INFO_NOT_COMPLETED.getMsg();
        }
        //判断前后密码是否一致
        if (!merchantRegisterVo.getPasswd().equals(merchantRegisterVo.getConfirmPasswd())) return RegiFailEnum.FAIL_PASSWD_DIFFERNET.getMsg();
        //判断账号是否在数据库中唯一
        QueryWrapper<Merchant> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account",merchantRegisterVo.getAccount());
        Merchant merchant = baseMapper.selectOne(queryWrapper);
        if (merchant != null) return RegiFailEnum.FAIL_ACCOUNT_REPEATED.getMsg();

        //插入数据
        Merchant merchant1 = new Merchant();
        merchant1.setAccount(merchantRegisterVo.getAccount());
        merchant1.setPasswd( DigestUtils.md5DigestAsHex(merchantRegisterVo.getPasswd().getBytes()));
        baseMapper.insert(merchant1);
        return null;
    }

    @Override
    public MerchantLoginDto login(MerchantLoginVo merchantLoginVo) {
        MerchantLoginDto merchantLoginDto = new MerchantLoginDto();
        if (!VertifyUtil.isInfoComplete(merchantLoginVo)){
            merchantLoginDto.setMsg(LoginFailEnum.FAIL_INFO_NOT_COMPLETED.getMsg());
            return merchantLoginDto;
        }
        //登录校验
        String encodedPasswd = DigestUtils.md5DigestAsHex(merchantLoginVo.getPasswd().getBytes());
        QueryWrapper<Merchant> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account",merchantLoginVo.getAccount())
                .eq("passwd",encodedPasswd);

        Merchant merchant = baseMapper.selectOne(queryWrapper);
        if (merchant == null){
            merchantLoginDto.setMsg(LoginFailEnum.FAIL_PASSWD_OR_ACCOUNT_ERROR.getMsg());
            return merchantLoginDto;
        }
        //生成token
        merchant.setToken("admin-token");
        merchantLoginDto.setMerchant(merchant);
        return merchantLoginDto;
    }

    @Override
    public Boolean uploadAuthInfo(Merchant merchant) {
        Merchant merchant1 = baseMapper.selectById(merchant.getId());
        if (merchant1 == null){
            return false;
        }
        merchant.setUpdateTime(new Date());
        merchant.setAuthStatus(StatusEnum.AUTH_UPDER_REVIEW.getCode());
        baseMapper.updateById(merchant); // null字段将不会修改
        return true;
    }

    @Override
    public Boolean updateStatus(Long id, Integer status) {
        if (baseMapper.selectById(id) == null) return false;
        Merchant merchant = new Merchant();
        merchant.setId(id);
        merchant.setStatus(status);
        baseMapper.updateById(merchant);
        return true;
    }

    @Override
    public Boolean updateAuthStatus(Long id, Integer authStatus) {
        if (baseMapper.selectById(id) == null) return false;
        Merchant merchant = new Merchant();
        merchant.setId(id);
        merchant.setAuthStatus(authStatus);
        baseMapper.updateById(merchant);
        return true;
    }
}
